﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IntroduceCompany.Singleton;
using IntroduceCompany.Util;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;


namespace IntroduceCompany.Models
{

    public class QuestionView_Validation
    {
        //[System.ComponentModel.DataAnnotations.MetadataType(typeof(IntroduceCompany.Models.QuestionView_Validation))]

        [Required(ErrorMessage = "Thứ tự xuất hiện câu hỏi phải có.")]
        [RegularExpression(ConstantClass.testPositiveInteger, ErrorMessage = "Thứ tự xuất hiện câu hỏi phải là dạng số nguyên dương.")]
        public String Location { get; set; }

        [Required(ErrorMessage = "Nội dung câu hỏi phải có.")]
        public String QuestionContent { get; set; }

    }


    public class AnswerView_Validation
    {
        //[System.ComponentModel.DataAnnotations.MetadataType(typeof(IntroduceCompany.Models.AnswerView_Validation))]

        [Required(ErrorMessage = "Nội dung câu trả lời phải có.")]
        public String AnswerContent { get; set; }

    }


    public class SurveyModels
    {

        /// <summary>
        /// Get question list filter by language
        /// </summary>
        /// <param name="languageID"></param>
        /// <returns></returns>
        internal IQueryable<QuestionView> GetQuestionList_Backend(string languageID)
        {
            if (languageID == ConstantClass.markViewAllLanguage)
            {
                return DataSingleton.GetInstance().QuestionViews.OrderByDescending(q => q.QuestionID);
            }
            else
            {
                return DataSingleton.GetInstance().QuestionViews.Where(q => q.LanguageID == languageID).OrderByDescending(q => q.QuestionID);
            }
        }

        /// <summary>
        /// Delete Question
        /// </summary>
        /// <param name="questionID"></param>
        internal void DeleteQuestion(int questionID)
        {
            try
            {
                DataSingleton.GetInstance().Questions.DeleteOnSubmit(DataSingleton.GetInstance().Questions.SingleOrDefault(q => q.QuestionID == questionID));
                DataSingleton.GetInstance().Questions.Context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DataSingleton.RefeshInstance();
            }
        }


        /// <summary>
        /// Update Visible column of Question
        /// </summary>
        /// <param name="questionID"></param>
        /// <param name="visible"></param>
        internal void ChangeQuestionVisible(int questionID, bool visible)
        {
            try
            {
                Question ques = DataSingleton.GetInstance().Questions.SingleOrDefault(q => q.QuestionID == questionID);
                ques.QuestionVisible = !visible;

                DataSingleton.GetInstance().Questions.Context.SubmitChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DataSingleton.RefeshInstance();
            }
        }

        /// <summary>
        /// Insert new question into database
        /// </summary>
        /// <param name="questionView"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        internal int CreateQuestion(QuestionView questionView, String username)
        {
            int newQuestionID = 0;
            try
            {
                newQuestionID = DataSingleton.GetInstance().sp_QuestionInsert(questionView.Location, username);

                Question_L temQuesL = new Question_L();
                temQuesL.QuestionID = newQuestionID;
                temQuesL.LanguageID = questionView.LanguageID;
                temQuesL.QuestionContent = questionView.QuestionContent.Trim();

                DataSingleton.GetInstance().Question_Ls.InsertOnSubmit(temQuesL);
                DataSingleton.GetInstance().Question_Ls.Context.SubmitChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DataSingleton.RefeshInstance();
            
                return 0;
            }
            return newQuestionID;
        }


        /// <summary>
        /// Get a record in question view filter by questionID and languageID
        /// </summary>
        /// <param name="questionID"></param>
        /// <param name="languageID"></param>
        /// <returns></returns>
        internal QuestionView GetOneQuestionView(int questionID, string languageID)
        {
            try
            {
                return DataSingleton.GetInstance().QuestionViews.SingleOrDefault(q => q.QuestionID == questionID && q.LanguageID == languageID);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }

        }
        
        /// <summary>
        /// Get a record in question table
        /// </summary>
        /// <param name="questionID"></param>
        /// <returns></returns>
        internal Question GetQuestionByQuestionID(int questionID)
        {
            return DataSingleton.GetInstance().Questions.SingleOrDefault(q => q.QuestionID == questionID);
        }


        /// <summary>
        /// Update Question
        /// </summary>
        /// <param name="questionView"></param>
        /// <param name="username"></param>
        internal bool UpdateQuestion(QuestionView questionView, string username)
        {
            try
            {
                Question temQues = DataSingleton.GetInstance().Questions.SingleOrDefault(q => q.QuestionID == questionView.QuestionID);
                temQues.Location = questionView.Location;
                temQues.QuestionUpdateDate = DateTime.Now;
                temQues.UserName = username;

                Question_L temQuesL = DataSingleton.GetInstance().Question_Ls.SingleOrDefault(q => q.QuestionID == questionView.QuestionID
                    && q.LanguageID == questionView.LanguageID);
                temQuesL.QuestionContent = questionView.QuestionContent.Trim();

                DataSingleton.GetInstance().SubmitChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DataSingleton.RefeshInstance();
            
                return false;
            }
            return true;
        }


        /// <summary>
        /// Insert new language for question
        /// </summary>
        /// <param name="questionView"></param>
        /// <returns></returns>
        internal bool InsertQuestionLanguage(QuestionView questionView)
        {
            try
            {
                Question_L temQuesL = new Question_L();
                temQuesL.QuestionID = questionView.QuestionID;
                temQuesL.LanguageID = questionView.LanguageID;
                temQuesL.QuestionContent = questionView.QuestionContent.Trim();

                DataSingleton.GetInstance().Question_Ls.InsertOnSubmit(temQuesL);
                DataSingleton.GetInstance().Question_Ls.Context.SubmitChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DataSingleton.RefeshInstance();
            
                return false;
            }
            return true;
        }



        /// <summary>
        /// Get Answer List
        /// </summary>
        /// <param name="languageID"></param>
        /// <returns></returns>
        internal IQueryable<AnswerView> GetAnswerList_Backend(int questionID, string languageID)
        {
            if (languageID == ConstantClass.markViewAllLanguage)
            {
                return DataSingleton.GetInstance().AnswerViews.Where(q => q.QuestionID == questionID);
            }
            else
            {
                return DataSingleton.GetInstance().AnswerViews.Where(q => q.LanguageID == languageID && q.QuestionID == questionID);
            }
        }

        /// <summary>
        /// Delete a record Answer
        /// </summary>
        /// <param name="answerID"></param>
        internal void DeleteAnswer(int answerID)
        {
            try
            {
                Answer temAns = DataSingleton.GetInstance().Answers.SingleOrDefault(a => a.AnswerID == answerID);
                DataSingleton.GetInstance().Answers.DeleteOnSubmit(temAns);
                DataSingleton.GetInstance().Answers.Context.SubmitChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DataSingleton.RefeshInstance();
            }
        }


        /// <summary>
        /// Insert new answer
        /// </summary>
        /// <param name="answerView"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        internal int CreateAnswer(AnswerView answerView, string username)
        {
            int newAnswerID = 0;
            try
            {
                newAnswerID = DataSingleton.GetInstance().sp_AnswerInsert(answerView.QuestionID, username);

                Answer_L temAnsL = new Answer_L();
                temAnsL.AnswerID = newAnswerID;
                temAnsL.LanguageID = answerView.LanguageID;
                temAnsL.AnswerContent = answerView.AnswerContent.Trim();

                DataSingleton.GetInstance().Answer_Ls.InsertOnSubmit(temAnsL);
                DataSingleton.GetInstance().Answer_Ls.Context.SubmitChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DataSingleton.RefeshInstance();
            
                return 0;
            }
            return newAnswerID;
        }


        /// <summary>
        /// Get a record of AnswerView
        /// </summary>
        /// <param name="answerID"></param>
        /// <param name="languageID"></param>
        /// <returns></returns>
        internal AnswerView GetOneAnswerView(int answerID, string languageID)
        {
            try{

                return DataSingleton.GetInstance().AnswerViews.SingleOrDefault(a => a.AnswerID == answerID && a.LanguageID == languageID);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }


        /// <summary>
        /// Get a record of Answer table
        /// </summary>
        /// <param name="answerID"></param>
        /// <returns></returns>
        internal Answer GetAnswerByAnswerID(int answerID)
        {
            return DataSingleton.GetInstance().Answers.SingleOrDefault(a => a.AnswerID == answerID);
        }


        /// <summary>
        ///  Update Answer
        /// </summary>
        /// <param name="answerView"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        internal bool UpdateAnswer(AnswerView answerView, string username)
        {
            try
            {
                Answer temAns = DataSingleton.GetInstance().Answers.SingleOrDefault(a => a.AnswerID == answerView.AnswerID);
                temAns.AnswerUpdateDate = DateTime.Now;
                temAns.UserName = username;

                Answer_L temAnsL = DataSingleton.GetInstance().Answer_Ls.SingleOrDefault(a => a.AnswerID == answerView.AnswerID && a.LanguageID == answerView.LanguageID);
                temAnsL.AnswerContent = answerView.AnswerContent.Trim();

                DataSingleton.GetInstance().SubmitChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DataSingleton.RefeshInstance();
            
                return false;
            }
            return true;
        }


        /// <summary>
        /// Inser another language for answer
        /// </summary>
        /// <param name="answerView"></param>
        /// <returns></returns>
        internal bool InsertAnswerLanguage(AnswerView answerView)
        {
            try
            {
                Answer_L temAnsL = new Answer_L();
                temAnsL.AnswerID = answerView.AnswerID;
                temAnsL.LanguageID = answerView.LanguageID;
                temAnsL.AnswerContent = answerView.AnswerContent.Trim();

                DataSingleton.GetInstance().Answer_Ls.InsertOnSubmit(temAnsL);
                DataSingleton.GetInstance().Answer_Ls.Context.SubmitChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DataSingleton.RefeshInstance();
            
                return false;
            }
            return true;
        }


        /// <summary>
        /// Checking answer of this language that is existed or not. True is existed or otherwise
        /// </summary>
        /// <param name="answerView"></param>
        /// <returns></returns>
        internal bool CheckAnswerLanguageIsExisted(AnswerView answerView)
        {
            return DataSingleton.GetInstance().Answer_Ls.Any(a => a.AnswerID == answerView.AnswerID && a.LanguageID == answerView.LanguageID);
        }


        /// <summary>
        /// Checking question of this language that is existed or not. True is existed or otherwise
        /// </summary>
        /// <param name="questionView"></param>
        /// <returns></returns>
        internal bool CheckQuestionLanguageIsExisted(QuestionView questionView)
        {
            return DataSingleton.GetInstance().Question_Ls.Any(q => q.QuestionID == questionView.QuestionID && q.LanguageID == questionView.LanguageID);
        }


        /// <summary>
        /// Get question content
        /// </summary>
        /// <param name="questionID"></param>
        /// <param name="languageID"></param>
        /// <returns></returns>
        internal String GetQuestionContent(int questionID, string languageID)
        {
            try
            {
                return DataSingleton.GetInstance().Question_Ls.SingleOrDefault(q => q.QuestionID == questionID && q.LanguageID == languageID).QuestionContent;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "";
            }
        }


        /// <summary>
        /// Get Question list except invisible question 
        /// </summary>
        /// <returns></returns>
        internal IQueryable<QuestionView> GetQuestionList_Frontend(String languageID)
        {
            return DataSingleton.GetInstance().QuestionViews.Where(q => q.QuestionVisible == true && q.TotalAnswer != 0 && q.LanguageID == languageID);
        }

////////////////////////////////////////////////////////// Thiện
        /// <summary>
        /// Get answers based on questionID 
        /// </summary>
        /// <returns></returns>
        public IQueryable<AnswerView> GetAnswerList_Frontend(int questionID,String languageID)
        {
            return DataSingleton.GetInstance().AnswerViews.Where(u=>u.QuestionID == questionID && u.LanguageID == languageID);
        }

        internal void UpdateTotalChooser(int answerID)
        {
            try{
            Answer temAns = DataSingleton.GetInstance().Answers.SingleOrDefault(a => a.AnswerID == answerID);
            temAns.ChoosenAmount = temAns.ChoosenAmount + 1;
           
            DataSingleton.GetInstance().SubmitChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                DataSingleton.RefeshInstance();
            }
        }
/////////////////////////////////////////////////////////


        internal IQueryable<QuestionView> GetQuestionList_Frontent(string languageID)
        {
            return DataSingleton.GetInstance().QuestionViews.Where(q => q.LanguageID == languageID && q.QuestionVisible == true).OrderBy(q => q.Location);
        }
    }
}